package cn.iocoder.yudao.module.pm.controller.admin.qcrecords;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pm.controller.admin.qcrecords.vo.QcRecordsPageReqVO;
import cn.iocoder.yudao.module.pm.controller.admin.qcrecords.vo.QcRecordsRespVO;
import cn.iocoder.yudao.module.pm.controller.admin.qcrecords.vo.QcRecordsSaveReqVO;
import cn.iocoder.yudao.module.pm.dal.dataobject.qcrecords.QcRecordsDO;
import cn.iocoder.yudao.module.pm.service.qcrecords.QcRecordsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 过程管理-速冷处理记录")
@RestController
@RequestMapping("/pm/qc-records")
@Validated
public class QcRecordsController {

    @Resource
    private QcRecordsService qcRecordsService;

    @PostMapping("/create")
    @Operation(summary = "创建过程管理-速冷处理记录")
    @PreAuthorize("@ss.hasPermission('pm:qc-records:create')")
    public CommonResult<Long> createQcRecords(@Valid @RequestBody QcRecordsSaveReqVO createReqVO) {
        return success(qcRecordsService.createQcRecords(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新过程管理-速冷处理记录")
    @PreAuthorize("@ss.hasPermission('pm:qc-records:update')")
    public CommonResult<Boolean> updateQcRecords(@Valid @RequestBody QcRecordsSaveReqVO updateReqVO) {
        qcRecordsService.updateQcRecords(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除过程管理-速冷处理记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pm:qc-records:delete')")
    public CommonResult<Boolean> deleteQcRecords(@RequestParam("id") Long id) {
        qcRecordsService.deleteQcRecords(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除过程管理-速冷处理记录")
                @PreAuthorize("@ss.hasPermission('pm:qc-records:delete')")
    public CommonResult<Boolean> deleteQcRecordsList(@RequestParam("ids") List<Long> ids) {
        qcRecordsService.deleteQcRecordsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得过程管理-速冷处理记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pm:qc-records:query')")
    public CommonResult<QcRecordsRespVO> getQcRecords(@RequestParam("id") Long id) {
        QcRecordsDO qcRecords = qcRecordsService.getQcRecords(id);
        return success(BeanUtils.toBean(qcRecords, QcRecordsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得过程管理-速冷处理记录分页")
    @PreAuthorize("@ss.hasPermission('pm:qc-records:query')")
    public CommonResult<PageResult<QcRecordsRespVO>> getQcRecordsPage(@Valid QcRecordsPageReqVO pageReqVO) {
        PageResult<QcRecordsDO> pageResult = qcRecordsService.getQcRecordsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QcRecordsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出过程管理-速冷处理记录 Excel")
    @PreAuthorize("@ss.hasPermission('pm:qc-records:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQcRecordsExcel(@Valid QcRecordsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QcRecordsDO> list = qcRecordsService.getQcRecordsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "过程管理-速冷处理记录.xls", "数据", QcRecordsRespVO.class,
                        BeanUtils.toBean(list, QcRecordsRespVO.class));
    }

}